#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstring>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "comm.h"
using namespace std;

bool MakeFifo()
{
    int n = mkfifo(FILENAME, 0666);

    if(n < 0)
    {
        cerr << "errno:" << errno << ",errstring:" << strerror(errno) << endl;
        return false;
    }
    cout << "Make fifo success" << endl;
    return true;
}
int main()
{
Start:
    int rfd = open(FILENAME, O_RDONLY);
    if(rfd < 0)
    {
        cerr << "errno:" << errno << ",errstring:" << strerror(errno) << endl;
        if(MakeFifo()) goto Start;
        else return 1;
    }
    
    char buffer[1048];
    while(true)
    {
        ssize_t s = read(rfd, buffer, sizeof(buffer) -1);
        if(s > 0)
        {
            buffer[s] = 0;
            cout << "Client Say:" << buffer << endl;
        }
        else 
        {
            cout << "Client quit" << endl;
        }
    }
    close(rfd);
    return 0;
}